package com.mars.push.core.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.mars.push.core.constant.Constant;
import com.mars.push.core.httpclient.HttpClientService;
import com.mars.push.core.request.PushRequest;
import com.mars.push.core.service.PushService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class XiaomiPushServiceImpl implements PushService {

    @Value("${push.xiaomi.appSecret}")
    private String appSecret;

    @Resource
    private HttpClientService httpClientService;

    @Override
    public boolean singlePush(PushRequest req) throws Exception {
        String accessToken = getAccessToken();
        JSONObject json = new JSONObject();
        json.put("title", req.getTitle());
        //0 表示通知栏消息 1 表示透传消息
        json.put("pass_through", 0);
        json.put("restricted_package_name", "com.imlianka.lkapp");
        json.put("description", req.getContent());
        json.put("notify_type", -1);
        json.put("registration_id", req.getRegisterId());
        json.put("payload", req.getTitle());
        //“1″：通知栏点击后打开app的Launcher Activity。  “2″：通知栏点击后打开app的任一Activity（开发者还需要传入extra.intent_uri）。
        json.put("extra.notify_effect", "2");
        json.put("extra.intent_uri", "intent://com.imlianka.lkapp/guide?" + req.getExtras() + "#Intent;scheme=pushscheme;launchFlags=0x4000000;end");
        JSONObject jsonObject = httpClientService.doPostJson(Constant.XIAO_MI_PUSH_URL, "xiaomi", accessToken, json);
        log.info("jsonObject===>" + jsonObject);
        return false;
    }

    @Override
    public String getAccessToken() {
        return appSecret;
    }
}
